package com.zxl.grocerymicroservicewechatmeal.controller;

import com.zxl.grocerymicroservicewechatmeal.dataobject.ProductCategory;
import com.zxl.grocerymicroservicewechatmeal.dataobject.ProductInfo;
import com.zxl.grocerymicroservicewechatmeal.service.ProductCategoryService;
import com.zxl.grocerymicroservicewechatmeal.service.ProductInfoService;
import com.zxl.grocerymicroservicewechatmeal.utils.ResultVOUtil;
import com.zxl.grocerymicroservicewechatmeal.vo.ProductInfoVO;
import com.zxl.grocerymicroservicewechatmeal.vo.ProductVO;
import com.zxl.grocerymicroservicewechatmeal.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zxl
 * @Description: 买家商品
 * @Date: Created in 21:57 2017/11/15
 * @Modified By:
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/test")
    public String Test(){
        return "qqqqq";
    }

    @GetMapping("/list")
    @Cacheable(cacheNames = "product", key = "1245245")
//    @Cacheable(cacheNames = "product", key = "#sellerId", condition ="#sellerId.length() > 3", unless = "#result.getCode() != 0")// key：动态取方法参数中指定字段的值（SpEl表达式http://www.importnew.com/17724.html）； condition:判断是否成立的表达式,条件成立缓存，否则不缓存；unless:根据返回结果进行缓存，code为0不缓存（错误的数据不缓存）
//    public ResultVO list(@RequestParam("sellerId") String sellerId) {
    public ResultVO list() {
        //1. 查询所有的上架商品
        List<ProductInfo> productInfoList = productInfoService.findUpAll();

        //2. 查询类目(1、查需要的，2、一次性查询)
//        List<Integer> categoryTypeList = new ArrayList<>();
        //传统方法
//        for (ProductInfo productInfo : productInfoList) {
//            categoryTypeList.add(productInfo.getCategoryType());
//        }
        //精简方法(java8, lambda)
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(e -> e.getCategoryType())
                .collect(Collectors.toList());
        List<ProductCategory> productCategoryList = productCategoryService.findByCategoryTypeIn(categoryTypeList);

        //3. 数据拼装
        List<ProductVO> productVOList = new ArrayList<>();
        for (ProductCategory productCategory: productCategoryList) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryType(productCategory.getCategoryType());
            productVO.setCategoryName(productCategory.getCategoryName());

            List<ProductInfoVO> productInfoVOList = new ArrayList<>();
            for (ProductInfo productInfo: productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);//把productInfo的值拷贝到productInfoVO
                    productInfoVOList.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOList);
            productVOList.add(productVO);
        }

        return ResultVOUtil.success(productVOList);
    }
}

